package psl;

import java.util.HashMap;
import java.util.ArrayList;
import java.sql.ResultSet;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Mar 21, 2007
 * Time: 6:34:33 PM
 */
public class UserAccount extends BasePage
{
    public String msCategory = null;
    public String msStatus = null;
    public String msOrderNo = null;
    public String msOdateFrom = null;
    public String msOdateTo = null;
    public String msOQtyFrom = null;
    public String msOQtyTo = null;
    public String msProPriceFrom = null;
    public String msProPriceTo = null;
    public boolean mbflag = false;

    public UserAccount ()
    {
        msViewPage = "UserAccount.jsp";
        msDetailPageId = "userAccountDetail";
    }

    public void setRequest (HashMap fhRequest)
    {
        msCategory = (String) fhRequest.get ("category");
        msOrderNo = (String) fhRequest.get ("orderno");
        msOdateFrom = (String) fhRequest.get ("odatefrom");
        msOdateTo = (String) fhRequest.get ("odateto");
        msOQtyFrom = (String) fhRequest.get ("oqtyfrom");
        msOQtyTo = (String) fhRequest.get ("oqtyto");
        msProPriceFrom = (String) fhRequest.get ("propricefrom");
        msProPriceTo = (String) fhRequest.get ("propriceto");
        msStatus = (String) fhRequest.get ("ostatus");
    }

    public void performContinue (String[] fsValue)
    {
        for (int i = 0; i < fsValue.length; i++)
        {
            if (moUserSession.mvSelsected != null)
            {
                if (moUserSession.mvSelsected.contains (fsValue[i]))
                {

                }
                else
                {
                    moUserSession.mvSelsected.add (fsValue[i]);
                }
            }
        }

        if (moUserSession.mvSelsected != null)
        {
            for (int i = 0; i < moUserSession.mvSelsected.size (); i++)
            {
                ArrayList lvQty = new ArrayList ();
                try
                {
                    String lsQry = "select quantity from tempdata where product_id='" + moUserSession.mvSelsected.get (i) + "'";
                    //String[][] lavalue=DBConn.setRecordsToString ("select quantity from tempdata",null,"product_id='"+moUserSession.mvSelsected.get(i)+"'");
                    ResultSet loResultSet = DBConn.getRecordFromDB (lsQry);
                    while (loResultSet.next ())
                    {
                        lvQty.add (loResultSet.getString (1));
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace ();
                }
                int liQty = 0;
                for (int j = 0; j < lvQty.size (); j++)
                {
                    liQty += Integer.parseInt ((String) lvQty.get (j));
                }
                if (moUserSession.mvQuantity != null && moUserSession.mvQuantity.size () != 0)
                {
                    moUserSession.mvQuantity.put (moUserSession.mvSelsected.get (i),liQty + "") ;
                }
                else
                {
                    moUserSession.mvQuantity = new HashMap ();
                    moUserSession.mvQuantity.put (moUserSession.mvSelsected.get (i),liQty + "") ;
                }
            }
        }
    }

    public void setConstant (HashMap fhRequest)
    {
        String[] laAction = (String[]) fhRequest.get ("action_fld");
        String fsTag=laAction[1];
        if (fsTag.equals ("next"))
        {
            miEndRow = (miRows - miStartRow < 10) ? (miStartRow + (miRows - miStartRow)) : (miStartRow + 10);
        }
        if (fsTag.equals ("start"))
        {
            miStartRow = 0;
            miEndRow = (miRows - miStartRow < 10) ? (miStartRow + (miRows - miStartRow)) : (miStartRow + 10);
        }
        if (fsTag.equals ("end"))
        {
            miStartRow= (miRows - miStartRow < 10) ? ((miRows%10)<0?0:(miRows-(miRows%10))):(miRows-(miRows%10));
            miEndRow = (miRows - miStartRow < 10) ? (miStartRow + (miRows - miStartRow)) : (miStartRow + 10);
        }
        if (fsTag.equals ("previous"))
        {
            miStartRow = miStartRow<20 ?0:(miEndRow%10!=0?miStartRow - ((miRows-miEndRow)+10):miStartRow - 20);
            miEndRow = (miRows - miStartRow < 10) ? (miStartRow + (miRows - miStartRow)) : (miStartRow + 10);
        }
    }

    public void performSearch ()
    {
        miStartRow=0;
        mbflag = true;
        mbSaveFlag=true;
        maRows=null;
        msCondition="";
        moUserSession.msError = null;
        if (msCategory != null && !msCategory.equals (""))
        {
            char ch = msCategory.charAt (0);
            String cha = ch + "";
            cha = cha.toUpperCase ();
            String lsStrinng = msCategory.substring (1);
            msCategory = cha + lsStrinng;
            msCondition += " AND category LIKE '%" + msCategory + "%'";
        }
        if (msOrderNo != null && !msOrderNo.equals (""))
        {
            char ch = msOrderNo.charAt (0);
            String cha = ch + "";
            cha = cha.toUpperCase ();
            String lsStrinng = msOrderNo.substring (1);
            msOrderNo = cha + lsStrinng;
            msCondition += " AND order_no LIKE '%" + msOrderNo + "%'";
        }
        if (msStatus != null && !msStatus.equals (""))
        {
            msCondition += " AND status LIKE '" + msStatus + "'";
        }

        if (msOdateFrom != null && !msOdateFrom.equals (""))
        {
            if (!CommonServices.isDate (msOdateFrom))
            {
                moUserSession.msError = "Please Enter Valid Date";
                return;
            }
            else
            {
                if (msOdateTo != null && !msOdateTo.equals (""))
                {
                    if (!CommonServices.isDate (msOdateTo))
                    {
                        moUserSession.msError = "Please Enter Valid Date";
                        return;
                    }
                    else
                    {
                        msCondition += " AND order_date BETWEEN TO_DATE('" + msOdateFrom + "', '" + AppConstants.SQL_DATE_FORMATE + "') " +
                                "AND TO_DATE('" + msOdateTo + "', '" + AppConstants.SQL_DATE_FORMATE + "')";
                    }
                }
                else
                {
                    msCondition += " AND order_date=TO_DATE('" + msOdateFrom + "', '" + AppConstants.SQL_DATE_FORMATE + "')";
                }
            }
        }
        if (msOQtyFrom != null && !msOQtyFrom.equals (""))
        {
            if (CommonServices.isNaN (msOQtyFrom))
            {
                moUserSession.msError = "Please Enter Valid Quantity";
                return;
            }
            else
            {
                if (msOQtyTo != null && !msOQtyTo.equals (""))
                {
                    if (CommonServices.isNaN (msOQtyTo))
                    {
                        moUserSession.msError = "Please Enter Valid Quantity";
                        return;
                    }
                    else
                    {
                        msCondition += " AND quantity BETWEEN '" + msOQtyFrom + "' AND '" + msOQtyTo + "'";
                    }
                }
                else
                {
                    msCondition += " AND quantity='" + msOQtyFrom + "'";
                }
            }
        }
        if (msProPriceFrom != null && !msProPriceFrom.equals (""))
        {
            if (CommonServices.isNaN (msProPriceFrom))
            {
                moUserSession.msError = "Please Enter Valid Price";
                return;
            }
            else
            {
                if (msProPriceTo != null && !msProPriceTo.equals (""))
                {
                    if (CommonServices.isNaN (msProPriceFrom))
                    {
                        moUserSession.msError = "Please Enter Valid Price";
                        return;
                    }
                    else
                    {
                        msCondition += " AND product_price BETWEEN '" + msProPriceFrom + "' AND '" + msProPriceTo + "'";
                    }
                }
                else
                {
                    msCondition += " AND product_price ='" + msProPriceFrom + "'";
                }

            }
        }
    }
}
